package com.jindy.module.project.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.project.domain.Project;
import com.jindy.module.project.service.ProjectService;
import com.jindy.system.service.ISysUserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目Controller
 *
 * @author zhaoxi
 * @date 2022-11-15
 */

@Api(tags = "业务后台 - 项目管理 ")
@RestController
@RequestMapping("/module/project")
@Validated
@Slf4j
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询项目数据列表
     */
    @PreAuthorize("@ss.hasPermi('module:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project){

        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        project.setUserId(sysUser.getUserId());
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 根据id查询项目信息
     */
    @PreAuthorize("@ss.hasPermi('module:project:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable("id") String id) {
        return AjaxResult.success(projectService.selectProjectById(id));
    }

    /**
     * 新增项目数据
     */
    @Log(title = "新增项目", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:project:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Project project)
    {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目数据
     */
    @Log(title = "修改项目", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:project:update')")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Project project) {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:project:delete')")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(projectService.deleteProjectById(id));
    }

    /**
     * 批量删除
     */
    @Log(title = "批量删除", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:project:deletes')")
    @DeleteMapping("/{ids}")
    public AjaxResult removes(@PathVariable String[] ids)
    {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
}
